<?php

/*
***
***分类商品列表
***
*/
class ListAction extends Action{
	public function index(){
		$Goods=M('Goods');
		$Cat=M('Goods_cat');
		$Model=M('Goods_model_value');
		$Ads=M('Ads');

		//推荐商品
		$this->goods_rec=$Goods->where("goods_label='rec' and is_post='0'")->limit(5)->select();
		//广告
		$this->leftad=$Ads->where("place_tag='listad01' and is_show='true'")->order("sorting")->limit(2)->select();

		$cat=$this->_get('cid');
		$kuozho=$this->_get('check');
		$cat_now=$Cat->where('cat_id='.$cat)->find();

		$kuozho=explode('-',$kuozho);
		for($j=0;$j<count($kuozho);$j++){
			if($kuozho[$j]!=''&&$kuozho[$j]!='全部'){
				$shuxing[]=$kuozho[$j];
			}
		}
		$shuxing=implode('%',$shuxing);
		if(!empty($shuxing)){
		$map['e_goods.pdt']=array('like','%'.$shuxing.'%');
		}

		if(!empty($cat)){
			$map['e_goods_cat.path']=array('like','%,'.$cat.'%');
		}
		$model=$Model->where('model_id='.$cat_now['model'])->select();
		for($i=0;$i<count($model);$i++){
			if($model[$i]['leixing']=='b'){
				$model[$i]['value']=json_decode($model[$i]['value_key']);
			}
		}

		import("@.ORG.Page");

		$goods=$Goods->join('e_goods_cat ON e_goods.cat_id=e_goods_cat.cat_id')->order('e_goods.goods_id DESC')->where($map)->select();
		$this->num=$Goods->join('e_goods_cat ON e_goods.cat_id=e_goods_cat.cat_id')->order('e_goods.goods_id DESC')->where($map)->count();
		$param = array(
			'result'=>$goods,			//分页用的数组或sql
			'listvar'=>'goods',			//分页循环变量
			'listRows'=>C('LIST_PAGE_SIZE'),			//每页记录数
			'parameter'=>'',//url分页后继续带的参数
			'target'=>'goods_list',	//ajax更新内容的容器id，不带#
			'pagesId'=>'page',		//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
			'template'=>'List:ajaxlist',//ajax更新模板
		);
		$this->page($param);

        $this->goods_view=unserialize(stripslashes(cookie('goods_view')));
		$this->cat=R('Goods/goods_cat');
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->assign("cat_now", $cat_now);
		$this->assign("model",$model);
		$this->name=$cat_now['cat_name'].'-'.C('WEB_NAME');
		$this->user=$_SESSION['euname'];
		$this->display();
	}

	public function page($param) {
		extract($param);
		import("@.ORG.Page");
		//总记录数
		$flag = is_string($result);
		$listvar = $listvar ? $listvar : 'goods';
		$listRows = $listRows? $listRows : 21;
		if ($flag)
			$totalRows = M()->table($result . ' a')->count();
		else
			$totalRows = ($result) ? count($result) : 1;
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);

		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
    
	public function search(){
		$Goods=M('Goods');
		$Cat=M('Goods_cat');
		$Ads=M('Ads');

		//推荐商品
		$this->goods_rec=$Goods->where("goods_label='rec' and is_post='0'")->limit(5)->select();
		//广告
		$this->leftad=$Ads->where("place_tag='listad01' and is_show='true'")->order("sorting")->limit(2)->select();

		$key=str_replace('+',' ',$this->_get('keyword'));
		$n_key=$this->key_word($key);

		$map['goods_name']=array('like','%'.implode('%',$n_key).'%');

		import("@.ORG.Page");
		$goods=$Goods->where($map)->select();
		$this->goods_num = $Goods->where($map)->count();
	
		for($i=0;$i<count($goods);$i++){
			for($j=0;$j<count($n_key);$j++){
				$goods[$i]['goods_name']=str_replace($n_key[$j],'<span style="color:#cc0000;font-weight:bold">'.$n_key[$j].'</span>',$goods[$i]['goods_name']);
			}
		}

		$param = array(
			'result'=>$goods,			//分页用的数组或sql
			'listvar'=>'goods',			//分页循环变量
			'listRows'=>8,			//每页记录数
			'parameter'=>'',//url分页后继续带的参数
			'target'=>'goods_list',	//ajax更新内容的容器id，不带#
			'pagesId'=>'page',		//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
			'template'=>'List:ajaxlist',//ajax更新模板
		);
		$this->page($param);
 
        $this->goods_view=unserialize(stripslashes(cookie('goods_view')));
		$this->cat=R('Goods/goods_cat');
		$shop_cart=cookie('shopcart');
		$this->cart_num=!empty($shop_cart)?count(unserialize(stripslashes($shop_cart))):0;
		$this->assign("cat_now", $cat_now);
		$this->name= "搜索\"".$key.'"-'.C('WEB_NAME');
		$this->user=$_SESSION['euname'];
		$this->assign("keyword",$key);
		$this->display();
	}

	public function key_word($key){
		vendor('fenci.phpanalysis'); 
		$str = $key;
		$do_fork = $do_unit = true;
		$do_multi = $do_prop = $pri_dict = false;

		if($str != '')
		{
			$tall = microtime(true);
			
			//初始化类
			PhpAnalysis::$loadInit = false;
			$pa = new PhpAnalysis('utf-8', 'utf-8', $pri_dict);
			//载入词典
			$pa->LoadDict();
			//执行分词
			$pa->SetSource($str);
			$pa->differMax = $do_multi;
			$pa->unitWord = $do_unit;
			$pa->StartAnalysis( $do_fork );
			
			$okresult = $pa->GetFinallyResult(' ', $do_prop);
	
			$pa_foundWordStr = $pa->foundWordStr;
			
			$slen = strlen($str);
			$slen = sprintf('%0.2f', $slen/1024);
			$pa = '';
			
		}

		$re=explode(' ',$okresult);
		array_splice($re,0,1);

		for($i=0;$i<count($re);$i++){
			if((strlen($re[$i])<4)&&(!eregi("[^\x80-\xff]",$re[$i]))){
			   array_splice($re,$i,1);
			   $i=0;
			}
		}
		return $re;
	}
	//end

}